<template>
	<view class="zone">
		<Header title="天道传认证"></Header>
		<view class="main">
			<view class="tigh">
				天道传认证
			</view>
			<view class="box">
				<view class="mingc">验证码</view>
				<view class="nickname">
					<input type="text" v-model="invite_code" class="weui-input" placeholder-class="wena"
						placeholder="输入验证码(邀请码)">
				</view>

				<view class="btn" @click="onSubmit">
					确认绑定
				</view>
			</view>


		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				wx_code: '',
				invite_code: '',
			}
		},
		onLoad(option) {
			console.log(option);

		},
		onShow() {
			this.auth();
		},
		methods: {
			//获取hash路由下的code
			getUrlCode() {
				// 截取url中的code方法
				var url = window.location.href; //获取打开的公众号的路径
				let winUrl = url;
				var theRequest = new Object();
				if (url.indexOf('?') != -1) {
					var str = url.substr(url.indexOf('?') + 1);
					var strs = str.split('&');
					for (var i = 0; i < strs.length; i++) {
						var items = strs[i].split('=');
						theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
					}
				} else {}
				return theRequest;
			},

			//授权的逻辑
			//授权接口
			auth() {
				let d_url = window.location.href;
				let red_url = encodeURIComponent(d_url)
				this.wx_code = this.getUrlCode().code;
				let appid = 'wx94dbfc94da98be43' //填写项目需要的appid，在开发者工具调试
				if (this.wx_code == null || this.wx_code === '' || this.wx_code == undefined) {
					var wxUrl =
						'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
						appid +
						'&redirect_uri=' +
						red_url +
						'&response_type=code' +
						'&scope=snsapi_userinfo' +
						'&state=STATE#wechat_redirect';
					window.location.href = wxUrl;
				}
			},

			onSubmit() {
				//这里是提交事件 根据自己的情况写

				if (this.invite_code == '' || this.invite_code == null) {
					uni.showToast({
						title: '请填写您的验证码',
						duration: 2000,
						icon: 'none'
					});
				} else {
					uni.request({
						method: 'POST',
						url: 'http://tdzh.cpgm8.cn/index.php/api/wx/callback',
						data: {
							invite_code: this.invite_code,
							code: this.wx_code
						},
						header: {
							"Content-Type": "application/json;charset=utf-8",
							'x-requested-with': 'XMLHttpRequest',
						},
						success: (res) => {
							if (res.code == 1) {
								uni.showToast({
									title: res.data.msg,
									duration: 2000,
									icon: 'none'
								});
							} else if (res.code == -10) {
								console.log('shuaxin');
								uni.showToast({
									title: res.data.msg,
									duration: 2000,
									icon: 'none'
								});
								setTimeout(() => {
									window.location.href ='http://tdzh.cpgm8.cn/h5/#/pages/index/shouquan';
								}, 1500)
							} else {
								console.log('shuaxin2');
								uni.showToast({
									title: res.data.msg,
									duration: 2000,
									icon: 'none'
								});
								setTimeout(() => {
									window.location.href ='http://tdzh.cpgm8.cn/h5/#/pages/index/shouquan';
								}, 1500)
							}
						}
					});
				}
			}
		}
	}
</script>

<style lang="less">
	.zone {
		background: url(http://image.qxgm.site/tdz/img/public/p_bg.png) no-repeat;
		background-size: 100% 100%;
		min-height: 100vh;


		.main {
			width: 100%;
			padding: 5vh 42px 0px;
			box-sizing: border-box;
			background: #e9e2dd;
			height: calc(100vh - 70px);
			overflow-y: scroll;
		}

		.box {
			position: relative;
			box-sizing: border-box;
		}

		.tigh {
			font-weight: normal;
			font-size: 21px;
			color: #0099FF;
			line-height: 24px;
			text-align: center;
			margin-bottom: 36px;
		}

		.nickname {
			padding: 7px 0px;
			text-align: center;
			background: #bcb8ba;
			border-radius: 1px;
			border: 1px solid rgba(51, 51, 51, 0.95);

			.weui-input {
				font-weight: normal;
				font-size: 18px;
				color: #666666;
				line-height: 18px;
			}

			.wena {
				font-weight: normal;
				font-size: 18px;
				color: #666666;
				line-height: 18px;
			}
		}

		.mingc {
			font-weight: normal;
			font-size: 18px;
			color: #333333;
			line-height: 18px;
			margin-left: 11px;
			margin-bottom: 11px;
		}
	}

	button {
		margin-top: 20px;
	}

	.btn {
		display: block;
		margin: 5vh auto 0;
		width: 116px;
		height: 37px;
		text-align: center;
		line-height: 37px;
		font-size: 18px;
		font-weight: normal;
		color: #fdf0c4;
		text-shadow: 0 1px 1px #CB6500;
		background: url(http://image.qxgm.site/tdz/img/youchai/mg-04.png) no-repeat;
		background-size: 100% 100%;
	}
</style>